//
//  SaveTool.m
//  UI19_豆瓣练习
//
//  Created by dllo on 15/10/13.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "SaveTool.h"
#import "ZGSCartoonHistoryModel.h"
#import "ZGSHomeCartoonModel.h"

@implementation SaveTool


+ (void)isHaveFolder:(SaveToolType)type {
    switch (type) {
        case SaveToolCartoonBookCase: {
            NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
            NSString *movieFile = [sandBoxPath stringByAppendingPathComponent:@"BookCase"];
            NSFileManager *manager = [NSFileManager defaultManager];
            if (![manager fileExistsAtPath:movieFile]) {
                [manager createDirectoryAtPath:movieFile withIntermediateDirectories:YES attributes:nil error:nil];
            }
            break;
        }
        case SaveToolCartoonHistory: {
            NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            NSString *movieFile = [sandBoxPath stringByAppendingPathComponent:@"CartoonHistory"];
            NSFileManager *manager = [NSFileManager defaultManager];
            if (![manager fileExistsAtPath:movieFile]) {
                [manager createDirectoryAtPath:movieFile withIntermediateDirectories:YES attributes:nil error:nil];
            }
            break;
        }
        default:
            break;
    }
    
}


+ (void)saveFileToPlist:(id)object type:(SaveToolType)type {
    
    switch (type) {
        case SaveToolCartoonBookCase: {
            ZGSCartoonHistoryModel *cartoonHistory = object;
            ZGSHomeCartoonModel *cartoon = cartoonHistory.cartoon;
            
            NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
            NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"BookCase/bookCase.plist"];

            NSMutableArray *bookCaseArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
            if (bookCaseArr == nil) {
                // 如果为空,先进行初始化
                bookCaseArr = [NSMutableArray array];
            }
            
            NSInteger count = 1;
            for (NSInteger i = 0; i < bookCaseArr.count; i++) {
                ZGSHomeCartoonModel *tempCartoon = [bookCaseArr[i] cartoon];
                if ([cartoon.albumId isEqualToNumber:tempCartoon.albumId]) {
                    [bookCaseArr replaceObjectAtIndex:i withObject:cartoonHistory];
                    [NSKeyedArchiver archiveRootObject:bookCaseArr toFile:docPath];
                    count ++;
                }
            }
            
            if (count == 1) {
                [bookCaseArr addObject:cartoonHistory];
                // 归档
                [NSKeyedArchiver archiveRootObject:bookCaseArr toFile:docPath];
                break;
            }
            break;
        }
            
        case SaveToolCartoonHistory: {
            ZGSCartoonHistoryModel *cartoonHistory = object;
            ZGSHomeCartoonModel *cartoon = cartoonHistory.cartoon;
            
            NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"CartoonHistory/history.plist"];
            // 直接归档会覆盖掉之前的数据,所以先进行反归档,然后填加进去之后再进行归档操作
            NSMutableArray *historyArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
            if (historyArr == nil) {
                // 如果为空,先进行初始化
                historyArr = [NSMutableArray array];
            }
            
            NSArray *arr = [NSArray arrayWithArray:historyArr];
            
            for (ZGSCartoonHistoryModel *tempch in arr) {
                ZGSHomeCartoonModel *tempCartoon = tempch.cartoon;
                if ([cartoon.albumId isEqualToNumber:tempCartoon.albumId]) {
                    [historyArr removeObject:tempch];
                }
            }
            
            [historyArr addObject:cartoonHistory];
            // 归档
            [NSKeyedArchiver archiveRootObject:historyArr toFile:docPath];
            break;
        }
            
        default:
            break;
    }
}

#if 1

+ (void)cancleFileInPlist:(id)object type:(SaveToolType)type {
    
    switch (type) {
        case SaveToolCartoonBookCase: {
            
            NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
            NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"BookCase/bookCase.plist"];
            // 反归档
            NSMutableArray *bookCaseArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
            
            ZGSCartoonHistoryModel *cartoonHistory = object;
            ZGSHomeCartoonModel *cartoon = cartoonHistory.cartoon;
            
            // 遍历之后移除相应的对象
            for (ZGSCartoonHistoryModel *tempch in bookCaseArr) {
                ZGSHomeCartoonModel *tempCartoon = tempch.cartoon;
                if ([cartoon.albumId isEqualToNumber:tempCartoon.albumId]) {
                    [bookCaseArr removeObject:tempch];
                    break;
                }
            }
            [NSKeyedArchiver archiveRootObject:bookCaseArr toFile:docPath];
            break;
        }
            
        case SaveToolCartoonHistory: {
            NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"CartoonHistory/history.plist"];
            // 反归档
            NSMutableArray *historyArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
            
            ZGSCartoonHistoryModel *cartoonHistory = object;
            ZGSHomeCartoonModel *cartoon = cartoonHistory.cartoon;
            
            // 遍历之后移除相应的对象
            for (ZGSCartoonHistoryModel *tempch in historyArr) {
                ZGSHomeCartoonModel *tempCartoon = tempch.cartoon;
                if ([cartoon.albumId isEqualToNumber:tempCartoon.albumId]) {
                    [historyArr removeObject:tempch];
                    break;
                }
            }
            [NSKeyedArchiver archiveRootObject:historyArr toFile:docPath];
            break;
        }
            
            
        default:
            break;
    }
}

+ (BOOL)isHaveInFile:(id)object type:(SaveToolType)type {
    
    switch (type) {
        case SaveToolCartoonBookCase: {
            // 拼接电影的文件夹路径
            NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
            NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"BookCase/bookCase.plist"];
            NSMutableArray *bookCaseArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];

            ZGSHomeCartoonModel *cartoon = object;
            
            for (ZGSCartoonHistoryModel *tempch in bookCaseArr) {
                ZGSHomeCartoonModel *tempCartoon = tempch.cartoon;
                if ([cartoon.albumId isEqualToNumber:tempCartoon.albumId]) {
                    return YES;
                    break;
                }
            }
            break;
        }
        
        case SaveToolCartoonHistory: {
            // 拼接电影的文件夹路径
            NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"CartoonHistory/history.plist"];
            NSMutableArray *historyArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
 
            NSNumber *albumId = object;
            
            for (ZGSCartoonHistoryModel *tempch in historyArr) {
                ZGSHomeCartoonModel *tempCartoon = tempch.cartoon;
                if ([albumId isEqualToNumber:tempCartoon.albumId]) {
                    return YES;
                    break;
                }
            }
            break;
        }
            
            
        default:
            break;
    }
    return NO;
}

+ (id)getFile:(id)object type:(SaveToolType)type {
    switch (type) {
        case SaveToolCartoonBookCase: {
            // 拼接电影的文件夹路径
            NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
            NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"BookCase/bookCase.plist"];
            NSMutableArray *bookCaseArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
  
            ZGSHomeCartoonModel *cartoon = object;
            
            for (ZGSCartoonHistoryModel *tempch in bookCaseArr) {
                ZGSHomeCartoonModel *tempCartoon = tempch.cartoon;
                if ([cartoon.albumId isEqualToNumber:tempCartoon.albumId]) {
                    return tempch;
                    break;
                }
            }
            break;
        }
            
        case SaveToolCartoonHistory: {
            // 拼接电影的文件夹路径
            NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"CartoonHistory/history.plist"];
            NSMutableArray *historyArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];

            NSNumber *albumId = object;
            
            for (ZGSCartoonHistoryModel *tempch in historyArr) {
                ZGSHomeCartoonModel *tempCartoon = tempch.cartoon;
                if ([albumId isEqualToNumber:tempCartoon.albumId]) {
                    return tempch;
                }
            }
            break;
        }
            
            
        default:
            break;
    }
    return nil;
}

#endif

@end
